package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author wangyc
 * @date 2025-08-14
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo vo = new NursingPlanVo();

        //查询护理计划数据
        NursingPlan nursingPlan = getById(id);
        //使用工具类，实现快速的属性名相同，源对象与目标对象的属性值同步赋值
        BeanUtils.copyProperties(nursingPlan, vo);

        //查询关联护理项目列表
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.getNursingProjectPlanMapperById(id);
        vo.setProjectPlans(projectPlans);

        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        //创建一个护理计划对象
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setSortNo(nursingPlanDto.getSortNo());
        nursingPlan.setPlanName(nursingPlanDto.getPlanName());
        nursingPlan.setStatus(nursingPlanDto.getStatus());
        //保存护理计划
        save(nursingPlan);


        //获取计划列表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            //循环遍历
            for (NursingProjectPlan projectPlan : projectPlans) {
                //获取计划表的主键id 赋值给到关系表中
                projectPlan.setPlanId(nursingPlan.getId());
            }
            //保存项目计划关系表
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }
        return 1;

    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = new NursingPlan();

        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //修改护理计划数据
        updateById(nursingPlan);


        //修改关联护理项目列表

        //获取关联护理项目列表数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //判断是否为空
        if (projectPlans != null && !projectPlans.isEmpty()) {

            LambdaQueryWrapper<NursingProjectPlan> wrapper = Wrappers.<NursingProjectPlan>lambdaQuery()
                    .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
            //首先先清空关联计划id的护理项目
            nursingProjectPlanMapper.delete(wrapper);

            //然后保存添加
            //循环遍历
            for (NursingProjectPlan projectPlan : projectPlans) {
                //获取计划表的主键id 赋值给到关系表中
                projectPlan.setPlanId(nursingPlan.getId());
            }
            //保存项目计划关系表
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }

        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Long id) {

        //删除关联护理项目列表
        LambdaQueryWrapper<NursingProjectPlan> wrapper = Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id);
        //首先先清空关联计划id的护理项目
        nursingProjectPlanMapper.delete(wrapper);

        //删除护理计划数据
        return removeById(id) ? 1 : 0;
    }
}
